package com.weigee.ktv.p.frag;

import android.os.Bundle;
import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.weigee.ktv.m.WeigeeKtv;
import com.weigee.ktv.m.retrofit.RetrofitUtil;
import com.weigee.ktv.p.TaskDetailContract;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by linde on 2017/5/10.
 * <p/>
 */
public class CommonProblemsPresenterImpl implements TaskDetailContract.CommonProblemsPresenter
{
    private final TaskDetailContract.CommonProblemsView view;

    public CommonProblemsPresenterImpl(TaskDetailContract.CommonProblemsView view)
    {
        this.view = view;
    }

    @Override
    public void onCreated(Bundle savedInstanceState)
    {

    }

    @Override
    public void onViewCreated(Bundle savedInstanceState)
    {

    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState)
    {
        onLoadListData(false);
    }

    @Override
    public void onStart()
    {

    }

    @Override
    public void onResume()
    {

    }

    @Override
    public void onPause()
    {

    }

    @Override
    public void onStop()
    {

    }

    @Override
    public void onDestroy()
    {

    }

    @Override
    public void onLoadData(Object... objects)
    {

    }

    @Override
    public void onLoadListData(final boolean moreData)
    {
        RetrofitUtil.INSTANCE.getApiService().commonProblemList().enqueue(new Callback<ResponseBody>()
        {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response)
            {
                try
                {
                    final String result = response.body().string();
                    if (!TextUtils.isEmpty(result))
                    {
                        JSONArray array = JSON.parseArray(result);
                        view.onResponseListData(array, moreData, array != null && array.size() >= WeigeeKtv.pageLength());
                    }
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t)
            {
                t.printStackTrace();
            }
        });
    }
}
